<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><title>Vue第二天 | XC's Blog</title><meta name="keywords" content="Vue框架笔记"><meta name="author" content="XC's Blog"><meta name="copyright" content="XC's Blog"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1. 天气案例-计算属性实现-computed-methods12345678910111213141516171819202122&lt;div class&#x3D;&quot;box&quot;&gt;    &lt;h1&gt;今天天气很 &amp;#x27;&amp;#123;&amp;#123;get&amp;#125;&amp;#125;&amp;#x27;&lt;&#x2F;h1&gt;    &lt;button @click&#x3D;&quot;sho"><meta property="og:type" content="article"><meta property="og:title" content="Vue第二天"><meta property="og:url" content="https://webxc.eu.org/post/aw92/index.html"><meta property="og:site_name" content="XC&#39;s Blog"><meta property="og:description" content="1. 天气案例-计算属性实现-computed-methods12345678910111213141516171819202122&lt;div class&#x3D;&quot;box&quot;&gt;    &lt;h1&gt;今天天气很 &amp;#x27;&amp;#123;&amp;#123;get&amp;#125;&amp;#125;&amp;#x27;&lt;&#x2F;h1&gt;    &lt;button @click&#x3D;&quot;sho"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png"><meta property="article:published_time" content="2023-07-16T03:00:00.000Z"><meta property="article:modified_time" content="2023-08-30T18:00:49.037Z"><meta property="article:author" content="XC&#39;s Blog"><meta property="article:tag" content="Vue框架笔记"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png"><link rel="shortcut icon" href="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><link rel="canonical" href="https://webxc.eu.org/post/aw92/"><link rel="preconnect" href="//cdn.jsdelivr.net"><link rel="preconnect" href="//busuanzi.ibruce.info"><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://npm.elemecdn.com/@fortawesome/fontawesome-free@6.2.0/css/all.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:void 0,localSearch:{path:"/search.xml",preload:!1,languages:{hits_empty:"找不到您查询的内容：${query}"}},translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlighjs",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:!1},copy:{success:"吾辈宣布,你的剪切板已被占领",error:"呀~复制失败,请在尝试下",noSupport:"对不起,你的浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"",date_suffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:{limitCount:60,languages:{author:"作者: XC's Blog",link:"链接: ",source:"来源: XC's Blog",info:"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},lightbox:"fancybox",Snackbar:{chs_to_cht:"您已切换为繁体",cht_to_chs:"您已切换为简体",day_to_night:"您已切换为深色主题",night_to_day:"您已切换为浅色主题",bgLight:"#3b70fc",bgDark:"#121212",position:"bottom-center"},source:{justifiedGallery:{js:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.min.js",css:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.css"}},isPhotoFigcaption:!1,islazyload:!0,isAnchor:!0}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"Vue第二天",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2023-08-30 18:00:49"}</script><noscript><style>#nav{opacity:1}.justified-gallery img{opacity:1}#post-meta time,#recent-posts time{display:inline!important}</style></noscript><script>(e=>{e.saveToLocal={set:function(e,t,o){if(0===o)return;const n=864e5*o,a={value:t,expiry:(new Date).getTime()+n};localStorage.setItem(e,JSON.stringify(a))},get:function(e){const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!((new Date).getTime()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=e=>new Promise(((t,o)=>{const n=document.createElement("script");n.src=e,n.async=!0,n.onerror=o,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,t())},document.head.appendChild(n)})),e.activateDarkMode=function(){document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=function(){document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme");"dark"===t?activateDarkMode():"light"===t&&activateLightMode();const o=saveToLocal.get("aside-status");void 0!==o&&("hide"===o?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><link rel="stylesheet" href="https://cdn1.tianli0.top/gh/web-xc/My-blog-CSS/mgi.css"><link rel="stylesheet" href="https://cdn1.tianli0.top/gh/web-xc/My-blog-CSS/mg111.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.16/css/mg2.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styless.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styles.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/font.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.15/css/font2.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiperstyle.css" media="print" onload='this.media="all"'><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">95</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-shouye"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-shijian"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw iconfont icon-fenlei"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-lianxiwomen"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-iconzhengli-"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw iconfont icon-a-wangzhanchaojilianjiehulianwang"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw iconfont icon-wangzhandingzhi"></i> <span>网址导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.webxc.eu.org/music/"><i class="fa-fw iconfont icon-musical-note"></i> <span>音乐解锁</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url('https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png')"><nav id="nav"><span id="blog_name"><div class="back-home-button" tabindex="-1"><i class="back-home-button-icon fas fa-grip-vertical"></i><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">项目</div><div class="back-menu-list"><a class="back-menu-item" href="/" title="前往博客主页" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">主站</span></a><a class="back-menu-item" href="https://e.webxc.eu.org/" rel="external nofollow" title="前往学习笔记" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">学习笔记</span></a><a class="back-menu-item" href="https://v.webxc.eu.org/" rel="external nofollow" title="VuePress学习文档" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">VuePress文档</span></a><a class="back-menu-item" href="https://b.webxc.eu.org/" rel="external nofollow" title="旧站Hexo" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">旧站Hexo</span></a><a class="back-menu-item" href="https://gridea.webxc.eu.org/" rel="external nofollow" title="Gridea笔记" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Gridea笔记</span></a><a class="back-menu-item" href="https://c.webxc.eu.org/" rel="external nofollow" title="魔改Hexo" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">魔改Hexo</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">博客镜像</div><div class="back-menu-list"><a class="back-menu-item" href="https://blog.webxc.eu.org/" title="Vercel镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Vercel镜像</span></a><a class="back-menu-item" href="https://webxc.tk/" title="Netlify镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Netlify镜像</span></a><a class="back-menu-item" href="https://web-xc.gitee.io/" title="Gitee镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Gitee镜像</span></a><a class="back-menu-item" href="https://cf.webxc.eu.org/" title="Cloudflare镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Cloudflare镜像</span></a><a class="back-menu-item" href="https://render.webxc.eu.org/" title="Render镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Render镜像</span></a></div></div></div></div><a id="site-name" href="/"><div class="title">XC's Blog</div><i class="fa-solid fa-house"></i></a></span><div class="mask-name-container"><center id="name-container"><a id="page-name" href="javascript:btf.scrollToDest(0, 500)">PAGE_NAME</a></center></div><div id="menus"><div class="nav-button" id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i> <span>搜索</span></a></div><div class="nav-button" id="darkmode_navswitch"><a class="darkmode_switchbutton" type="button" title="浅色和深色模式转换" onclick="switchDarkMode()"><i class="fas fa-adjust"></i></a></div><div class="nav-button" id="nav-totop"><a class="totopbtn"><i class="fas fa-arrow-up"></i><span id="percent" onclick="btf.scrollToDest(0,500)">0</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-shouye"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-shijian"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw iconfont icon-fenlei"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-lianxiwomen"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-iconzhengli-"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw iconfont icon-a-wangzhanchaojilianjiehulianwang"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw iconfont icon-wangzhandingzhi"></i> <span>网址导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.webxc.eu.org/music/"><i class="fa-fw iconfont icon-musical-note"></i> <span>音乐解锁</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Vue第二天</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-16T03:00:00.000Z" title="发表于 2023-07-16 03:00:00">2023-07-16</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-08-30T18:00:49.037Z" title="更新于 2023-08-30 18:00:49">2023-08-30</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Vue%E6%A1%86%E6%9E%B6%E7%AC%94%E8%AE%B0/">Vue框架笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span data-flag-title="Vue第二天"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="twikoo_visitors"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="1-天气案例-计算属性实现-computed-methods"><a href="#1-天气案例-计算属性实现-computed-methods" class="headerlink" title="1. 天气案例-计算属性实现-computed-methods"></a>1. 天气案例-计算属性实现-computed-methods</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>今天天气很 &#x27;&#123;&#123;get&#125;&#125;&#x27;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;show&quot;</span>&gt;</span>切换天气<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    &lt;!-- @click=<span class="string">&quot;xxx&quot;</span> 绑定事件时xxx里可以写一些简单语句--&gt;</span><br><span class="line">    &lt;!-- <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;name = !name&quot;</span>&gt;</span>切换天气<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> --&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="literal">true</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">computed</span>: &#123;</span><br><span class="line">        <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">name</span> ? <span class="string">&#x27;炎热&#x27;</span> : <span class="string">&#x27;凉爽&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">show</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">name</span> = !<span class="variable language_">this</span>.<span class="property">name</span></span><br><span class="line">        &#125;   </span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="2-天气案例-监听属性watch实现-immediate"><a href="#2-天气案例-监听属性watch实现-immediate" class="headerlink" title="2. 天气案例-监听属性watch实现-immediate"></a>2. 天气案例-监听属性watch实现-immediate</h2><ol><li>当被监视的属性变化时, 回调函数自动调用, 进行相关操作</li><li>监视的属性必须存在, 才能进行监视</li><li>监视属性的两种写法:</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>今天天气很 &#x27;&#123;&#123;get&#125;&#125;&#x27;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;show&quot;</span>&gt;</span>切换天气<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="literal">true</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">computed</span>: &#123;</span><br><span class="line">        <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">name</span> ? <span class="string">&#x27;炎热&#x27;</span> : <span class="string">&#x27;凉爽&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">show</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">name</span> = !<span class="variable language_">this</span>.<span class="property">name</span></span><br><span class="line">        &#125;   </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">watch</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: &#123;</span><br><span class="line">            <span class="attr">immediate</span>: <span class="literal">true</span>, <span class="comment">// 初始化时让name调用一下</span></span><br><span class="line">        <span class="comment">// 当nams发生改变时 handler会被调用</span></span><br><span class="line">            <span class="title function_">handler</span>(<span class="params">newvalue, oldvalue</span>) &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;name被修改了&#x27;</span>, newvalue, oldvalue)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;   </span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// vm.$watch(&#x27;name&#x27;, &#123;</span></span><br><span class="line"><span class="comment">//     immediate: true,</span></span><br><span class="line"><span class="comment">//     handler(newvalue, oldvalue) &#123;</span></span><br><span class="line"><span class="comment">//         console.log(&#x27;name被修改了&#x27;, newvalue, oldvalue)</span></span><br><span class="line"><span class="comment">//     &#125; </span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br></pre></td></tr></table></figure><h2 id="3-天气案例-深度监视实现-watch-deep"><a href="#3-天气案例-深度监视实现-watch-deep" class="headerlink" title="3. 天气案例-深度监视实现-watch-deep"></a>3. 天气案例-深度监视实现-watch-deep</h2><ol><li>Vue中的watch默认不监视对象内部值的改变(一层)</li><li>配置deep:true 可以监视对象内部值改变(多层)</li><li>Vue自身可以监视对象内部值的改变, 但Vue提供的watch对象默认不可以</li><li>使用watch时根据数据的具体结构, 决定是否采用深度监视</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>今天天气很 &#x27;&#123;&#123;get&#125;&#125;&#x27;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;show&quot;</span>&gt;</span>切换天气<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    &lt;hr&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>a的值是&#123;&#123;num.a&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;num.a++&quot;</span>&gt;</span>点我让a+1<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>b的值是&#123;&#123;num.b&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;num.b++&quot;</span>&gt;</span>点我让b+1<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;num= &#123;a: 666, b: 888&#125;&quot;</span>&gt;</span>点我彻底替换num<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="literal">true</span>,</span><br><span class="line">        <span class="attr">num</span>: &#123;</span><br><span class="line">            <span class="attr">a</span>: <span class="number">1</span>,</span><br><span class="line">            <span class="attr">b</span>: <span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">computed</span>: &#123;</span><br><span class="line">        <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">name</span> ? <span class="string">&#x27;炎热&#x27;</span> : <span class="string">&#x27;凉爽&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">show</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">name</span> = !<span class="variable language_">this</span>.<span class="property">name</span></span><br><span class="line">        &#125;   </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">watch</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: &#123;</span><br><span class="line">            <span class="title function_">handler</span>(<span class="params">newvalue, oldvalue</span>) &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;name被修改了&#x27;</span>, newvalue, oldvalue)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">// 监视多级结构中所有属性的变化</span></span><br><span class="line">        <span class="string">&#x27;num&#x27;</span>: &#123;</span><br><span class="line">            <span class="attr">deep</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="title function_">handler</span>(<span class="params"></span>) &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;num被改变了&#x27;</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="4-天气案例-深度监听简写"><a href="#4-天气案例-深度监听简写" class="headerlink" title="4. 天气案例-深度监听简写"></a>4. 天气案例-深度监听简写</h2><ul><li>不用deep和immediate属性时, 监听属性$watch才能简写</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>今天天气很 &#x27;&#123;&#123;get&#125;&#125;&#x27;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;show&quot;</span>&gt;</span>切换天气<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="literal">true</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">computed</span>: &#123;</span><br><span class="line">        <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">name</span> ? <span class="string">&#x27;炎热&#x27;</span> : <span class="string">&#x27;凉爽&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">show</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">name</span> = !<span class="variable language_">this</span>.<span class="property">name</span></span><br><span class="line">        &#125;   </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">watch</span>: &#123;</span><br><span class="line">    <span class="comment">// 1. 完整写法</span></span><br><span class="line">        <span class="comment">// name: &#123;</span></span><br><span class="line">        <span class="comment">//     handler(newvalue, oldvalue) &#123;</span></span><br><span class="line">        <span class="comment">//         console.log(&#x27;name被修改了&#x27;, newvalue, oldvalue)</span></span><br><span class="line">        <span class="comment">//     &#125;</span></span><br><span class="line">        <span class="comment">// &#125;</span></span><br><span class="line">    <span class="comment">// 2. 简写</span></span><br><span class="line">        <span class="title function_">name</span>(<span class="params">newvalue, oldvalue</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;name被修改了&#x27;</span>, newvalue, oldvalue)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 3. vm.$watch 的完整写法</span></span><br><span class="line"><span class="comment">// vm.$watch(&#x27;name&#x27;, &#123;</span></span><br><span class="line"><span class="comment">//     handler(newvalue, oldvalue) &#123;</span></span><br><span class="line"><span class="comment">//         console.log(&#x27;name被修改了&#x27;, newvalue, oldvalue)</span></span><br><span class="line"><span class="comment">//     &#125;</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 4. vm.$watch 的简写</span></span><br><span class="line"><span class="comment">// vm.$watch(&#x27;name&#x27;, function (newvalue, oldvalue) &#123;</span></span><br><span class="line"><span class="comment">//     console.log(&#x27;name被修改了&#x27;, newvalue, oldvalue)</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br></pre></td></tr></table></figure><h2 id="5-computed和watch之间的区别"><a href="#5-computed和watch之间的区别" class="headerlink" title="5. computed和watch之间的区别"></a>5. computed和watch之间的区别</h2><ol><li><p>computed能完成的功能, watch都可以完成</p></li><li><p>watch能完成的功能, computed不一定能完成, 比如: watch可以进行异步操作</p></li><li><p>重要原则:</p></li></ol><blockquote><ol><li>被Vue管理的函数, 最好写成普通函数, 这样this的指向才是vm或组件实例对象</li><li>所有不被Vue管理的函数, 最好写成箭头函数, 这样this的指向才是vm或组件实例对象</li><li>(定时器的回调函数, ajax的回调函数, Promise的回调函数等..)</li></ol></blockquote><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    姓: &lt;input type=&quot;text&quot; v-model=&quot;name&quot;&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">    名: &lt;input type=&quot;text&quot; v-model=&quot;names&quot;&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">    全名: &lt;span&gt;&#123;&#123;all&#125;&#125;&lt;/span&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">new Vue(&#123;</span><br><span class="line">    el: &#x27;.box&#x27;,</span><br><span class="line">    data: &#123;</span><br><span class="line">        name: &#x27;张&#x27;,</span><br><span class="line">        names: &#x27;三&#x27;,</span><br><span class="line">        all: &#x27;张-三&#x27;</span><br><span class="line">    &#125;,</span><br><span class="line">    watch: &#123;</span><br><span class="line">        name(value) &#123;</span><br><span class="line">            setTimeout(() =&gt; &#123;</span><br><span class="line">                this.all = value + &#x27;-&#x27; + this.names</span><br><span class="line">            &#125;, 1000)</span><br><span class="line">        &#125;,</span><br><span class="line">        names(value) &#123;</span><br><span class="line">            this.all = this.name + &#x27;-&#x27; + value</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="6-Vue绑定class样式-class"><a href="#6-Vue绑定class样式-class" class="headerlink" title="6. Vue绑定class样式 :class"></a>6. Vue绑定class样式 :class</h2><ul><li>:class&#x3D;”xxx” xxx可以是字符串、对象、数组</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    &lt;!-- <span class="number">1.</span>绑定<span class="keyword">class</span>样式: 字符串写法, 适用于 样式类名不确定, 需要动态指定 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;basic&quot;</span> <span class="attr">:class</span>=<span class="string">&quot;color&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;show&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    &lt;!-- <span class="number">2.</span>数组写法, 适用于 要绑定的样式不确定, 名字也不确定 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;basic&quot;</span> <span class="attr">:class</span>=<span class="string">&quot;arr&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    &lt;!-- <span class="number">3.</span>对象写法, 适用于 要绑定的样式个数确定, 名字也确定 但要动态决定用不用 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;basic&quot;</span> <span class="attr">:class</span>=<span class="string">&quot;all&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;小城&#x27;</span>,</span><br><span class="line">        <span class="attr">color</span>: <span class="string">&#x27;normal&#x27;</span>,</span><br><span class="line">        <span class="attr">arr</span>: [<span class="string">&#x27;n1&#x27;</span>, <span class="string">&#x27;n2&#x27;</span>, <span class="string">&#x27;n3&#x27;</span>],</span><br><span class="line">        <span class="attr">all</span>: &#123;</span><br><span class="line">            <span class="attr">n1</span>: <span class="literal">false</span>,</span><br><span class="line">            <span class="attr">n2</span>: <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">show</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">let</span> arr = [<span class="string">&#x27;happy&#x27;</span>, <span class="string">&#x27;sad&#x27;</span>, <span class="string">&#x27;normal&#x27;</span>]</span><br><span class="line">            <span class="keyword">let</span> index = <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>()*<span class="number">3</span>)</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">color</span> = arr[index]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="7-Vue绑定style样式-style"><a href="#7-Vue绑定style样式-style" class="headerlink" title="7. Vue绑定style样式 :style"></a>7. Vue绑定style样式 :style</h2><ol><li>:style&#x3D;”{fontSize: xxx}” 其中xxx是动态值</li><li>:style&#x3D;”[a, b]” 其中a,b是样式对象</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    &lt;!-- 绑定style样式: 对象写法 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;basic&quot;</span> <span class="attr">:style</span>=<span class="string">&quot;&#123;fontSize: size +&#x27;px&#x27;&#125;&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;basic&quot;</span> <span class="attr">:style</span>=<span class="string">&quot;fsize&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    &lt;!-- 绑定style样式: 数组写法 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;basic&quot;</span> <span class="attr">:style</span>=<span class="string">&quot;[fsize]&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;小城&#x27;</span>,</span><br><span class="line">        <span class="attr">size</span>: <span class="number">20</span>,</span><br><span class="line">        <span class="attr">fsize</span>: &#123;</span><br><span class="line">            <span class="attr">fontSize</span>: <span class="string">&#x27;30px&#x27;</span>,</span><br><span class="line">            <span class="attr">backgroundColor</span>: <span class="string">&#x27;pink&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="8-Vue条件渲染v-show-v-else-if-template模板"><a href="#8-Vue条件渲染v-show-v-else-if-template模板" class="headerlink" title="8. Vue条件渲染v-show-v-else-if-template模板"></a>8. Vue条件渲染v-show-v-else-if-template模板</h2><h3 id="1-v-show写法-v-show-x3D-”表达式”"><a href="#1-v-show写法-v-show-x3D-”表达式”" class="headerlink" title="1. v-show写法: v-show&#x3D;”表达式”"></a>1. v-show写法: v-show&#x3D;”表达式”</h3><blockquote><ol><li>表达式可以是布尔值、表达式、对象</li><li>适用于: 切换频率较高的场景</li><li>特点: 不展示的DOM元素只是隐藏了, 并未移除</li></ol></blockquote><h3 id="2-v-if-else写法"><a href="#2-v-if-else写法" class="headerlink" title="2. v-if-else写法:"></a>2. v-if-else写法:</h3><blockquote><ol><li>v-if&#x3D;”表达式”</li><li>v-else-if&#x3D;”表达式”</li><li>v-else&#x3D;”表达式”</li><li>v-if和template配合使用, template标签在页面是不会呈现的</li><li>表达式可以是布尔值、表达式、对象</li><li>适用于: 切换频率较低的场景</li><li>特点: 不展示的DOM元素直接移除</li><li>注意: v-if可以和v-else-if、v-else一起使用, 但要求结构不能被’打断’</li></ol></blockquote><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    &lt;!-- v-show条件渲染可以是布尔值 也可以是表达式 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">v-show</span>=<span class="string">&quot;true&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">v-show</span>=<span class="string">&quot;1 == 1&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    &lt;!-- v-show条件渲染也可以是对象 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">v-show</span>=<span class="string">&quot;a&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    &lt;!-- v-<span class="keyword">if</span>条件渲染可以是布尔值 也可以是表达式 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">v-if</span>=<span class="string">&quot;false&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">v-if</span>=<span class="string">&quot;1 == 1&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    &lt;!-- 使用v-<span class="keyword">if</span>条件渲染做交互 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>当前n的值是&#123;&#123;n&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;n++&quot;</span>&gt;</span>点我让n+1<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;n == 1&quot;</span>&gt;</span>1出来了<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;n == 2&quot;</span>&gt;</span>2出来了<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;n == 3&quot;</span>&gt;</span>3出来了<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    &lt;!-- v-<span class="keyword">else</span>和v-<span class="keyword">else</span>-<span class="keyword">if</span>的使用 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;n == 1&quot;</span>&gt;</span>1出来了<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-else-if</span>=<span class="string">&quot;n == 2&quot;</span>&gt;</span>2出来了<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-else</span>&gt;</span>没有了<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    &lt;!-- v-<span class="keyword">if</span>和template配合使用 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">template</span> <span class="attr">v-if</span>=<span class="string">&quot;n === 1&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>1哈哈哈<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>2哈哈哈<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>3哈哈哈<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;小城&#x27;</span>,</span><br><span class="line">        <span class="attr">a</span>: <span class="literal">true</span>,</span><br><span class="line">        <span class="attr">n</span>: <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="9-Vue列表渲染v-for指令"><a href="#9-Vue列表渲染v-for指令" class="headerlink" title="9. Vue列表渲染v-for指令"></a>9. Vue列表渲染v-for指令</h2><ol><li>用于展示列表数据</li><li>语法: v-for&#x3D;”(value, index) in xxx” :key&#x3D;”id”</li><li>可以遍历: 数组、对象、字符串、指定次数 后两个用的很少</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    &lt;!-- 遍历数组 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>遍历数组<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    &lt;!-- 遍历对象 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>遍历对象-汽车信息<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(value, key) of obj&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;key&quot;</span>&gt;</span>&#123;&#123;value&#125;&#125; - &#123;&#123;key&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    &lt;!-- 遍历字符串 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>遍历字符串<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(value, index) of str&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;value&#125;&#125; - &#123;&#123;index&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    &lt;!-- 遍历指定次数 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>遍历指定次数<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(value, index) of 5&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;value&#125;&#125; - &#123;&#123;index&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">arr</span>: [</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">19</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;王五&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span>&#125;</span><br><span class="line">        ],</span><br><span class="line">        <span class="attr">obj</span>: &#123;</span><br><span class="line">            <span class="attr">name</span>: <span class="string">&#x27;奥迪&#x27;</span>,</span><br><span class="line">            <span class="attr">price</span>: <span class="string">&#x27;50万&#x27;</span>,</span><br><span class="line">            <span class="attr">color</span>: <span class="string">&#x27;黑色&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">str</span>: <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="10-Vue列表渲染v-for的key作用与原理"><a href="#10-Vue列表渲染v-for的key作用与原理" class="headerlink" title="10. Vue列表渲染v-for的key作用与原理"></a>10. Vue列表渲染v-for的key作用与原理</h2><h3 id="1-虚拟DOM中key的作用"><a href="#1-虚拟DOM中key的作用" class="headerlink" title="1. 虚拟DOM中key的作用"></a>1. 虚拟DOM中key的作用</h3><blockquote><p>key是虚拟DOM对象的标识, 当数据发生变化时, Vue会根据新数据生成新的虚拟DOM</p><p>随后Vue进行新虚拟DOM与旧虚拟DOM的差异比较(Diff算法)</p></blockquote><h3 id="2-对比规则"><a href="#2-对比规则" class="headerlink" title="2. 对比规则"></a>2. 对比规则</h3><blockquote><p><strong>(1) 旧虚拟DOM中找到了与新虚拟DOM相同的key:</strong></p><p>若虚拟DOM中内容没变, 直接使用之前的真实DOM</p><p>若虚拟DOM中内容变了, 则生成新的真实DOM, 随后替换掉页面中之前的真实DOM</p><p><strong>(2) 旧虚拟DOM中未找到与新虚拟DOM相同的key:</strong></p><p>创建新的真实DOM, 随后渲染到页面</p></blockquote><h3 id="3-用index作为key可能会引发的问题"><a href="#3-用index作为key可能会引发的问题" class="headerlink" title="3. 用index作为key可能会引发的问题"></a>3. 用index作为key可能会引发的问题</h3><blockquote><p><strong>1. 若对数据进行: 逆序添加、删除等破环顺序操作:</strong></p><p>会产生没有必要的真实DOM &#x3D;&#x3D;&gt; 界面效果没问题, 但效率低</p><p><strong>2. 如果结构中还包含输入流的DOM:</strong></p><p>会产生错误DOM更新 &#x3D;&#x3D;&gt; 界面有问题</p></blockquote><h3 id="4-开发中如何选择key"><a href="#4-开发中如何选择key" class="headerlink" title="4. 开发中如何选择key?"></a>4. 开发中如何选择key?</h3><blockquote><p>1.最好使用每条数据的唯一标识作为key, 比如id、手机号、学号等唯一值</p><p>2.如果不存在对数据的逆序添加、删除等破坏顺序的操作, 仅用于渲染列表、展示,</p><p>使用index作为key是没有问题的</p></blockquote><h3 id="5-面试题-React、Vue中的key有什么作用-key内部原理"><a href="#5-面试题-React、Vue中的key有什么作用-key内部原理" class="headerlink" title="5. 面试题: React、Vue中的key有什么作用? (key内部原理)"></a>5. 面试题: React、Vue中的key有什么作用? (key内部原理)</h3><blockquote><p><strong>1. 虚拟DOM中key的作用:</strong></p><p>key是虚拟DOM对象的标识, 当数据发生变化时, Vue会根据新数据生成新的虚拟DOM</p><p>随后Vue进行新虚拟DOM与旧虚拟DOM的差异比较(Diff算法)</p></blockquote><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            &#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125;</span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;add&quot;</span>&gt;</span>添加一个老刘<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">arr</span>: [</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">19</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;王五&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">add</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">let</span> add = &#123;<span class="attr">id</span>: <span class="number">4</span>, <span class="attr">name</span>: <span class="string">&#x27;老刘&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>&#125;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">arr</span>.<span class="title function_">unshift</span>(add)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="11-Vue的列表过滤"><a href="#11-Vue的列表过滤" class="headerlink" title="11. Vue的列表过滤"></a>11. Vue的列表过滤</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in words&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            &#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125; - &#123;&#123;x.sex&#125;&#125;</span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    模糊搜索: <span class="language-xml"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;请输入名字&quot;</span> <span class="attr">v-model</span>=<span class="string">&quot;word&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">word</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        <span class="attr">arr</span>: [</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&#x27;马冬梅&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span>, <span class="attr">sex</span>: <span class="string">&#x27;女&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&#x27;周冬雨&#x27;</span>, <span class="attr">age</span>: <span class="number">19</span>, <span class="attr">sex</span>: <span class="string">&#x27;女&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;周杰伦&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span>, <span class="attr">sex</span>: <span class="string">&#x27;男&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;温兆伦&#x27;</span>, <span class="attr">age</span>: <span class="number">21</span>, <span class="attr">sex</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">computed</span>: &#123;</span><br><span class="line">        <span class="title function_">words</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">word</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">arr</span>.<span class="title function_">filter</span>(<span class="function">(<span class="params">index</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> index.<span class="property">name</span>.<span class="title function_">indexOf</span>(<span class="variable language_">this</span>.<span class="property">word</span>) !== -<span class="number">1</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// new Vue(&#123;</span></span><br><span class="line"><span class="comment">//     el: &#x27;.box&#x27;,</span></span><br><span class="line"><span class="comment">//     data: &#123;</span></span><br><span class="line"><span class="comment">//         word: &#x27;&#x27;,</span></span><br><span class="line"><span class="comment">//         words: [],</span></span><br><span class="line"><span class="comment">//         arr: [</span></span><br><span class="line"><span class="comment">//             &#123;id: 1, name: &#x27;马冬梅&#x27;, age: 18, sex: &#x27;女&#x27;&#125;,</span></span><br><span class="line"><span class="comment">//             &#123;id: 2, name: &#x27;周冬雨&#x27;, age: 19, sex: &#x27;女&#x27;&#125;,</span></span><br><span class="line"><span class="comment">//             &#123;id: 3, name: &#x27;周杰伦&#x27;, age: 20, sex: &#x27;男&#x27;&#125;,</span></span><br><span class="line"><span class="comment">//             &#123;id: 3, name: &#x27;温兆伦&#x27;, age: 21, sex: &#x27;男&#x27;&#125;</span></span><br><span class="line"><span class="comment">//         ]</span></span><br><span class="line"><span class="comment">//     &#125;,</span></span><br><span class="line"><span class="comment">//     watch: &#123;</span></span><br><span class="line"><span class="comment">//         // 1. watch的简写实现, 但有bug: 页面打开数据没有, 搜索正常</span></span><br><span class="line"><span class="comment">//         // word(value) &#123;</span></span><br><span class="line"><span class="comment">//         //     console.log(&#x27;word被改了&#x27;, value)</span></span><br><span class="line"><span class="comment">//         //     this.words = this.arr.filter((index) =&gt; &#123;</span></span><br><span class="line"><span class="comment">//         //         return index.name.indexOf(value) !== -1</span></span><br><span class="line"><span class="comment">//         //     &#125;)</span></span><br><span class="line"><span class="comment">//         // &#125;</span></span><br><span class="line"><span class="comment">//         // 2. 使用watch的完整写法实现, immediate可以默认调用一次</span></span><br><span class="line"><span class="comment">//         word: &#123;</span></span><br><span class="line"><span class="comment">//             immediate: true,</span></span><br><span class="line"><span class="comment">//             handler(value) &#123;</span></span><br><span class="line"><span class="comment">//                 console.log(&#x27;word被调用了&#x27;, value)</span></span><br><span class="line"><span class="comment">//                 this.words = this.arr.filter((index) =&gt; &#123;</span></span><br><span class="line"><span class="comment">//                     return index.name.indexOf(value) !== -1</span></span><br><span class="line"><span class="comment">//                 &#125;)</span></span><br><span class="line"><span class="comment">//             &#125;</span></span><br><span class="line"><span class="comment">//         &#125;</span></span><br><span class="line"><span class="comment">//     &#125;</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br></pre></td></tr></table></figure><h2 id="12-Vue的列表过滤与列表排序"><a href="#12-Vue的列表过滤与列表排序" class="headerlink" title="12. Vue的列表过滤与列表排序"></a>12. Vue的列表过滤与列表排序</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用computed计算属性实现</span></span><br><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in words&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            &#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125; - &#123;&#123;x.sex&#125;&#125;</span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    模糊搜索: <span class="language-xml"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;请输入名字&quot;</span> <span class="attr">v-model</span>=<span class="string">&quot;word&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;type = 2&quot;</span>&gt;</span>年龄升序<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;type = 1&quot;</span>&gt;</span>年龄降序<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;type = 0&quot;</span>&gt;</span>原顺序<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">word</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        <span class="attr">type</span>: <span class="number">0</span>, <span class="comment">// 0原顺序 1降序 2升序</span></span><br><span class="line">        <span class="attr">arr</span>: [</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&#x27;马冬梅&#x27;</span>, <span class="attr">age</span>: <span class="number">31</span>, <span class="attr">sex</span>: <span class="string">&#x27;女&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&#x27;周冬雨&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>, <span class="attr">sex</span>: <span class="string">&#x27;女&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;周杰伦&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span>, <span class="attr">sex</span>: <span class="string">&#x27;男&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;温兆伦&#x27;</span>, <span class="attr">age</span>: <span class="number">21</span>, <span class="attr">sex</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">computed</span>: &#123;</span><br><span class="line">        <span class="title function_">words</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">word</span>)</span><br><span class="line">            <span class="keyword">let</span> arr = <span class="variable language_">this</span>.<span class="property">arr</span>.<span class="title function_">filter</span>(<span class="function">(<span class="params">index</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> index.<span class="property">name</span>.<span class="title function_">indexOf</span>(<span class="variable language_">this</span>.<span class="property">word</span>) !== -<span class="number">1</span></span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="comment">// 判断是否需要排序 使用.sort方法操作</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">type</span>) &#123;</span><br><span class="line">                arr.<span class="title function_">sort</span>(<span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">type</span> == <span class="number">1</span> ? b.<span class="property">age</span>-a.<span class="property">age</span> : a.<span class="property">age</span>-b.<span class="property">age</span></span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> arr</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="13-data数据里的数组更新时的一个问题"><a href="#13-data数据里的数组更新时的一个问题" class="headerlink" title="13. data数据里的数组更新时的一个问题"></a>13. data数据里的数组更新时的一个问题</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Vue数据层面变了, 但未检测到数据改变</span></span><br><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            &#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125; - &#123;&#123;x.sex&#125;&#125;</span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;news&quot;</span>&gt;</span>更新马冬梅的信息<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">arr</span>: [</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&#x27;马冬梅&#x27;</span>, <span class="attr">age</span>: <span class="number">31</span>, <span class="attr">sex</span>: <span class="string">&#x27;女&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&#x27;周冬雨&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>, <span class="attr">sex</span>: <span class="string">&#x27;女&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;周杰伦&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span>, <span class="attr">sex</span>: <span class="string">&#x27;男&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;温兆伦&#x27;</span>, <span class="attr">age</span>: <span class="number">21</span>, <span class="attr">sex</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">news</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="comment">// this.arr[0].name = &#x27;马老师&#x27;</span></span><br><span class="line">            <span class="comment">// this.arr[0].age = 50</span></span><br><span class="line">            <span class="comment">// this.arr[0].sex = &#x27;男&#x27;</span></span><br><span class="line">            <span class="comment">// 通过数组下标直接赋值是不行的</span></span><br><span class="line">            <span class="comment">// this.arr[0] = &#123;id: 1, name: &#x27;马老师&#x27;, age: 50, sex: &#x27;男&#x27;&#125;</span></span><br><span class="line">            <span class="comment">// 这里使用数组.splice方法替换</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">arr</span>.<span class="title function_">splice</span>(<span class="number">0</span>, <span class="number">1</span>, &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&#x27;马老师&#x27;</span>, <span class="attr">age</span>: <span class="number">50</span>, <span class="attr">sex</span>: <span class="string">&#x27;男&#x27;</span>&#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="14-Vue检测数据改变原理"><a href="#14-Vue检测数据改变原理" class="headerlink" title="14. Vue检测数据改变原理"></a>14. Vue检测数据改变原理</h2><ul><li>Vue利用递归会一直在data属性里创建get&#x2F;set</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;names&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;小城&#x27;</span>,</span><br><span class="line">        <span class="attr">names</span>: <span class="string">&#x27;冲冲冲&#x27;</span>,</span><br><span class="line">        <span class="attr">a</span>: &#123;</span><br><span class="line">            <span class="attr">b</span>: <span class="string">&#x27;ok&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">c</span>: [<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="15-模拟Vue对象数据监测"><a href="#15-模拟Vue对象数据监测" class="headerlink" title="15. 模拟Vue对象数据监测"></a>15. 模拟Vue对象数据监测</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> data = &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;尚硅谷&#x27;</span>,</span><br><span class="line">    <span class="attr">age</span>: <span class="string">&#x27;北京&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(data)</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Observer</span>(<span class="params">obj</span>) &#123;</span><br><span class="line"><span class="comment">// 汇总对象所有属性形成一个数组</span></span><br><span class="line">    <span class="keyword">let</span> keys = <span class="title class_">Object</span>.<span class="title function_">keys</span>(obj)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(keys)</span><br><span class="line">    keys.<span class="title function_">forEach</span>(<span class="function">(<span class="params">k</span>) =&gt;</span> &#123;</span><br><span class="line">        </span><br><span class="line"><span class="comment">// 此处this指向Observer实例对象</span></span><br><span class="line">        <span class="title class_">Object</span>.<span class="title function_">defineProperty</span>(<span class="variable language_">this</span>,k, &#123;</span><br><span class="line">            <span class="comment">// 数据代理 </span></span><br><span class="line">            <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> obj[k]</span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="comment">// 数据劫持</span></span><br><span class="line">            <span class="title function_">set</span>(<span class="params">value</span>) &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;data属性被改了, 我去解析模板, 生成虚拟DOM了&#x27;</span>)</span><br><span class="line">                obj[k] = value</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 创建一个监视的实例对象, 用于监视data属性变化</span></span><br><span class="line"><span class="keyword">let</span> obs = <span class="keyword">new</span> <span class="title class_">Observer</span>(data)</span><br><span class="line"><span class="comment">// 准备一个vm实例对象 模拟Vue的_data</span></span><br><span class="line"><span class="keyword">let</span> vm = &#123;&#125;</span><br><span class="line">vm.<span class="property">_data</span> = data = obs</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obs)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(vm)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 死循环了</span></span><br><span class="line"><span class="comment">// Object.defineProperty(data, &#x27;age&#x27;, &#123;</span></span><br><span class="line"><span class="comment">//     get() &#123;</span></span><br><span class="line"><span class="comment">//         return data.age</span></span><br><span class="line"><span class="comment">//     &#125;,</span></span><br><span class="line"><span class="comment">//     set(val) &#123;</span></span><br><span class="line"><span class="comment">//         data.age = val</span></span><br><span class="line"><span class="comment">//     &#125;</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br></pre></td></tr></table></figure><h2 id="16-Vue-set方法的使用"><a href="#16-Vue-set方法的使用" class="headerlink" title="16. Vue.set方法的使用"></a>16. Vue.set方法的使用</h2><ol><li>Vue.set方法: <strong>向响应式对象添加属性</strong></li><li>如果想给后期追加的属性添加响应式处理, 调用以下两个方法都可以</li><li>语法: <strong>Vue.set(目标对象, ‘属性名’, ‘属性值’)</strong></li><li><strong>vm.$set(目标对象, 添加的属性名, 属性值)</strong></li><li>注意对象不能为Vue实例 或者Vue实例的根数据对象</li><li>向响应式对象中添加一个property, 并确保这个新property同样是响应式的, 且触发视图更新</li><li>它必须用于向响应式对象上添加新property, 因为Vue无法探测普通的新增property</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    &lt;hr&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;add&quot;</span>&gt;</span>点我添加一个性别属性, 默认值为男<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>学生姓名: &#123;&#123;obj.name&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span> <span class="attr">v-if</span>=<span class="string">&quot;obj.sex&quot;</span>&gt;</span>学生性别: &#123;&#123;obj.sex&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>学生年龄: 真实 &#123;&#123;obj.ages.age1&#125;&#125; 对外 &#123;&#123;obj.ages.age2&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>朋友们<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;小城&#x27;</span>,</span><br><span class="line">        <span class="attr">obj</span>: &#123;</span><br><span class="line">            <span class="attr">name</span>: <span class="string">&#x27;小明&#x27;</span>,</span><br><span class="line">            <span class="comment">// sex: &#x27;男&#x27;, // undefined不显示页面</span></span><br><span class="line">            <span class="attr">ages</span>: &#123;</span><br><span class="line">                <span class="attr">age1</span>: <span class="number">10</span>,</span><br><span class="line">                <span class="attr">age2</span>: <span class="number">20</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">arr</span>: [</span><br><span class="line">            &#123;<span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">40</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">add</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="comment">// Vue.set(this.obj, &#x27;sex&#x27;, &#x27;男&#x27;)</span></span><br><span class="line">            <span class="variable language_">this</span>.$set(<span class="variable language_">this</span>.<span class="property">obj</span>, <span class="string">&#x27;sex&#x27;</span>, <span class="string">&#x27;男&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="17-模拟Vue数组数据监测与原理"><a href="#17-模拟Vue数组数据监测与原理" class="headerlink" title="17. 模拟Vue数组数据监测与原理"></a>17. 模拟Vue数组数据监测与原理</h2><ul><li>通过数组下标修改数组中的元素, 默认情况是没有添加响应式处理的, 怎么解决?</li></ul><h3 id="1-第一种方案"><a href="#1-第一种方案" class="headerlink" title="1. 第一种方案"></a>1. 第一种方案</h3><blockquote><ol><li>通过 Vue.set(数组对象, 下标, 值)</li><li>通过 vm.$set(数组对象, 下标, 值)</li></ol></blockquote><h3 id="2-第二种方案"><a href="#2-第二种方案" class="headerlink" title="2. 第二种方案"></a>2. 第二种方案</h3><blockquote><p>通过这7个方法给数组添加响应式处理: push、pop、shift、unshift、splice、sort、reverse</p><p>Vue将被侦听的数组的变更方法进行了包裹, 所以它们也将会触发视图更新</p></blockquote><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    &lt;hr&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;add&quot;</span>&gt;</span>点我添加一个性别属性, 默认值为男<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>学生姓名: &#123;&#123;obj.name&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span> <span class="attr">v-if</span>=<span class="string">&quot;obj.sex&quot;</span>&gt;</span>学生性别: &#123;&#123;obj.sex&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>学生年龄: 真实 &#123;&#123;obj.ages.age1&#125;&#125; 对外 &#123;&#123;obj.ages.age2&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>朋友们<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    &lt;!-- 模拟<span class="title class_">Vue</span>数组数据监测 --&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>爱好<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in like&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;x&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;小城&#x27;</span>,</span><br><span class="line">        <span class="attr">obj</span>: &#123;</span><br><span class="line">            <span class="attr">name</span>: <span class="string">&#x27;小明&#x27;</span>,</span><br><span class="line">            <span class="comment">// sex: &#x27;男&#x27;,</span></span><br><span class="line">            <span class="attr">ages</span>: &#123;</span><br><span class="line">                <span class="attr">age1</span>: <span class="number">10</span>,</span><br><span class="line">                <span class="attr">age2</span>: <span class="number">20</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">like</span>: [<span class="string">&#x27;唱歌&#x27;</span>, <span class="string">&#x27;跳舞&#x27;</span>, <span class="string">&#x27;篮球&#x27;</span>],</span><br><span class="line">        <span class="attr">arr</span>: [</span><br><span class="line">            &#123;<span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>&#125;,</span><br><span class="line">            &#123;<span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">40</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">add</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="comment">// Vue.set(this.obj, &#x27;sex&#x27;, &#x27;男&#x27;)</span></span><br><span class="line">            <span class="variable language_">this</span>.$set(<span class="variable language_">this</span>.<span class="property">obj</span>, <span class="string">&#x27;sex&#x27;</span>, <span class="string">&#x27;男&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="18-总结Vue监测数据原理-x2F-对象-x2F-数组"><a href="#18-总结Vue监测数据原理-x2F-对象-x2F-数组" class="headerlink" title="18. 总结Vue监测数据原理&#x2F;对象&#x2F;数组"></a>18. 总结Vue监测数据原理&#x2F;对象&#x2F;数组</h2><h4 id="1-Vue监测数据的原理-Vue会监视data中所有层次的数据"><a href="#1-Vue监测数据的原理-Vue会监视data中所有层次的数据" class="headerlink" title="1. Vue监测数据的原理: Vue会监视data中所有层次的数据"></a>1. Vue监测数据的原理: Vue会监视data中所有层次的数据</h4><h3 id="2-如何监测对象中的数据"><a href="#2-如何监测对象中的数据" class="headerlink" title="2. 如何监测对象中的数据?"></a>2. 如何监测对象中的数据?</h3><blockquote><p>通过setter实现监视, 且要在new Vue时就传入要监测的数据</p><p>(1) 对象中 后追加的属性, Vue默认不做响应式处理</p><p>(2) 如果给 后添加的属性做响应式, 请使用以下API:</p><p>Vue.set(目标对象, 添加的属性名, 属性值) 或</p><p>vm.$set(目标对象, 添加的属性名, 属性</p></blockquote><h3 id="3-如何监测数组中的数据"><a href="#3-如何监测数组中的数据" class="headerlink" title="3. 如何监测数组中的数据?"></a>3. 如何监测数组中的数据?</h3><blockquote><p>通过包裹数组更新元素的方法实现, 本质就是做了两件事:</p><p>(1) 调用原生对应的JS方法对数组进行更新</p><p>(2) 重新解析模板, 进而更新页面</p></blockquote><h3 id="4-在Vue修改数组中的某个元素一定要用以下方法"><a href="#4-在Vue修改数组中的某个元素一定要用以下方法" class="headerlink" title="4. 在Vue修改数组中的某个元素一定要用以下方法:"></a>4. 在Vue修改数组中的某个元素一定要用以下方法:</h3><blockquote><p>使用这些API: push、pop、shift、unshift、splice、sort、reverse</p><p>Vue.set() 或 vm.$set()</p><p>注意: Vue.set 和 vm.$set 不能给vm或vm的根数据对象 添加属性 !!!</p></blockquote><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>姓名: &#123;&#123;xs.name&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>年龄: &#123;&#123;xs.age&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span> <span class="attr">v-if</span>=<span class="string">&quot;xs.sex&quot;</span>&gt;</span>性别: &#123;&#123;xs.sex&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>爱好: <span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in xs.like&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;x&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>朋友们: <span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;(x, index) in xs.arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span>&#123;&#123;x.name&#125;&#125; - &#123;&#123;x.age&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;xs.age++&quot;</span>&gt;</span>年龄+1岁<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addName&quot;</span>&gt;</span>添加性别属性: 默认值: 男<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;xs.sex = &#x27;女&#x27;&quot;</span>&gt;</span>修改性别属性: 女<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addArr&quot;</span>&gt;</span>在列表首位添加一个朋友<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addArrs&quot;</span>&gt;</span>修改第一个朋友名字为: 爱坤<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addLike&quot;</span>&gt;</span>添加一个爱好<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addLikes&quot;</span>&gt;</span>修改第一个爱好为: 开车<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span> &lt;br&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addFilter&quot;</span>&gt;</span>过滤掉爱好中的篮球<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">xs</span>: &#123;</span><br><span class="line">            <span class="attr">name</span>: <span class="string">&#x27;小城&#x27;</span>,</span><br><span class="line">            <span class="attr">age</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="attr">like</span>: [<span class="string">&#x27;唱歌&#x27;</span>, <span class="string">&#x27;跳舞&#x27;</span>, <span class="string">&#x27;篮球&#x27;</span>],</span><br><span class="line">            <span class="attr">arr</span>: [</span><br><span class="line">                &#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span> &#125;,</span><br><span class="line">                &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">40</span> &#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">addName</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="title class_">Vue</span>.<span class="title function_">set</span>(<span class="variable language_">this</span>.<span class="property">xs</span>, <span class="string">&#x27;sex&#x27;</span>, <span class="string">&#x27;男&#x27;</span>)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="title function_">addArr</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">xs</span>.<span class="property">arr</span>.<span class="title function_">unshift</span>(&#123;<span class="attr">name</span>: <span class="string">&#x27;蔡徐坤&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span>&#125;)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="title function_">addArrs</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="comment">// Vue.set(this.xs.arr[0], &#x27;name&#x27;, &#x27;爱坤&#x27;)</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">xs</span>.<span class="property">arr</span>[<span class="number">0</span>].<span class="property">name</span> = <span class="string">&#x27;爱坤&#x27;</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">xs</span>.<span class="property">arr</span>[<span class="number">0</span>].<span class="property">age</span> = <span class="number">10</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="title function_">addLike</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">xs</span>.<span class="property">like</span>.<span class="title function_">push</span>(<span class="string">&#x27;学习&#x27;</span>)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="title function_">addLikes</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="comment">// this.xs.like.splice(0, 1, &#x27;开车&#x27;)</span></span><br><span class="line">            <span class="comment">// Vue.set(this.xs.like, 0, &#x27;开车&#x27;)</span></span><br><span class="line">            <span class="variable language_">this</span>.$set(<span class="variable language_">this</span>.<span class="property">xs</span>.<span class="property">like</span>, <span class="number">0</span>, <span class="string">&#x27;开车&#x27;</span>)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="title function_">addFilter</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">xs</span>.<span class="property">like</span> = <span class="variable language_">this</span>.<span class="property">xs</span>.<span class="property">like</span>.<span class="title function_">filter</span>(<span class="function">(<span class="params">x</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> x !== <span class="string">&#x27;篮球&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="19-Vue的收集表单数据-v-model-lazy-number-trim"><a href="#19-Vue的收集表单数据-v-model-lazy-number-trim" class="headerlink" title="19. Vue的收集表单数据-v-model-lazy-number-trim"></a>19. Vue的收集表单数据-v-model-lazy-number-trim</h2><ol><li>若: input type&#x3D;”text” 则v-model收集的是value值, 用户输入的就是value值</li><li>若: input type&#x3D;”radio” 则v-model收集的是value值, 且要给标签配置value值</li><li>若: input type&#x3D;”checked”</li></ol><ul><li>(1) 没有配置input的value属性, 那么收集的就是checked(勾选true&#x2F;未勾选false)</li><li>(2) 配置input的value属性</li><li>v-model的初始值是非数组, 那么收集的就是checked(勾选true&#x2F;未勾选false)</li><li>v-model的初始值是数组, 那么收集的就是value组成的数组</li></ul><blockquote><p>备注: v-model的三个修饰符:</p><p>lazy: 失去焦点再收集数据</p><p>number: 输入字符串转换为number类型</p><p>trim: 空格过滤</p></blockquote><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><br><span class="line">    &lt;form action=&quot;&quot; @submit.prevent=&quot;arr&quot;&gt;</span><br><span class="line">        账号: &lt;input type=&quot;text&quot; v-model.trim=&quot;obj.user&quot;&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">        密码: &lt;input type=&quot;password&quot; v-model=&quot;obj.pass&quot;&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">        年龄: &lt;input type=&quot;number&quot; v-model.number=&quot;obj.age&quot;&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">        性别: </span><br><span class="line">        男: &lt;input type=&quot;radio&quot; name=&quot;sex&quot; v-model=&quot;obj.sex&quot; value=&quot;nan&quot;&gt;</span><br><span class="line">        女: &lt;input type=&quot;radio&quot; name=&quot;sex&quot; v-model=&quot;obj.sex&quot; value=&quot;nv&quot;&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">        爱好: </span><br><span class="line">        学习&lt;input type=&quot;checkbox&quot; v-model=&quot;obj.like&quot; value=&quot;xx&quot;&gt;</span><br><span class="line">        唱歌&lt;input type=&quot;checkbox&quot; v-model=&quot;obj.like&quot; value=&quot;cg&quot;&gt;</span><br><span class="line">        篮球&lt;input type=&quot;checkbox&quot; v-model=&quot;obj.like&quot; value=&quot;lq&quot;&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">        所属校区: </span><br><span class="line">        &lt;select v-model=&quot;obj.city&quot;&gt;</span><br><span class="line">            &lt;option value=&quot;&quot;&gt;请选择校区&lt;/option&gt;</span><br><span class="line">            &lt;option value=&quot;bj&quot;&gt;北京&lt;/option&gt;</span><br><span class="line">            &lt;option value=&quot;ss&quot;&gt;上海&lt;/option&gt;</span><br><span class="line">            &lt;option value=&quot;sz&quot;&gt;深圳&lt;/option&gt;</span><br><span class="line">            &lt;option value=&quot;wh&quot;&gt;武汉&lt;/option&gt;</span><br><span class="line">        &lt;/select&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">        其他信息: &lt;textarea v-model.lazy=&quot;obj.text&quot;&gt;&lt;/textarea&gt; &lt;br&gt;&lt;br&gt;</span><br><span class="line">        &lt;input type=&quot;checkbox&quot; v-model=&quot;obj.check&quot;&gt;阅读并接受&lt;a href=&quot;&quot;&gt;《用户协议》&lt;/a&gt;</span><br><span class="line">        &lt;button&gt;提交&lt;/button&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">new Vue(&#123;</span><br><span class="line">    el: &#x27;.box&#x27;,</span><br><span class="line">    data: &#123;</span><br><span class="line">        obj: &#123;</span><br><span class="line">            user: &#x27;&#x27;,</span><br><span class="line">            pass: &#x27;&#x27;,</span><br><span class="line">            age: &#x27;&#x27;,</span><br><span class="line">            sex: &#x27;nan&#x27;,</span><br><span class="line">            like: [],</span><br><span class="line">            city: &#x27;ss&#x27;,</span><br><span class="line">            text: &#x27;&#x27;,</span><br><span class="line">            check: &#x27;&#x27;  </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">        arr() &#123;</span><br><span class="line">            console.log(this._data.obj)</span><br><span class="line">            console.log(JSON.stringify(this._data.obj))</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h2 id="20-Vue的过滤器-filter-dayjs库"><a href="#20-Vue的过滤器-filter-dayjs库" class="headerlink" title="20. Vue的过滤器-filter-dayjs库"></a>20. Vue的过滤器-filter-dayjs库</h2><p>过滤器定义: 对要显示的数据进行特殊格式化后再显示(适应于一些简单逻辑处理)</p><h3 id="过滤器语法"><a href="#过滤器语法" class="headerlink" title="过滤器语法:"></a>过滤器语法:</h3><ol><li>注册过滤器: Vue.filter(name, function) 或 new Vue{filters: {}}</li><li>使用过滤器: {xxx | 过滤器名} 或 v-bind:属性 &#x3D; ‘xxx’ | 过滤器名</li></ol><h3 id="备注"><a href="#备注" class="headerlink" title="备注:"></a>备注:</h3><ol><li>过滤器也可以接收额外参数, 多个过滤器也可以串联</li><li>并没有改变原本的数据, 是产生新的对应数据</li><li>分全局过滤器、局部过滤器</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=<span class="string">&quot;https://cdn.bootcdn.net/ajax/libs/dayjs/1.11.7/dayjs.min.js&quot;</span>&gt;&lt;/script&gt;</span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>显示格式化后的时间<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;time&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="comment">&lt;!-- 计算属性实现 --&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>计算后的时间: &#123;&#123;times&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="comment">&lt;!-- methods方法实现 --&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>计算后的时间: &#123;&#123;times1()&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="comment">&lt;!-- Vue过滤器实现 --&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>计算后的时间: &#123;&#123;time | formater&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="comment">&lt;!-- Vue过滤器实现(传参) --&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>计算后的时间: &#123;&#123;time | formater(&#x27;YYYY-MM-DD&#x27;)&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="comment">&lt;!-- Vue过滤器实现(传参 + 过滤器串联) --&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>计算后的时间: &#123;&#123;time | formater(&#x27;YYYY-MM-DD&#x27;) | myslice&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="comment">&lt;!-- Vue过滤器 应用在v-bind上(很少) --&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span> <span class="attr">:value</span>=<span class="string">&quot;name | myslice&quot;</span>&gt;</span>尚硅谷<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box1&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="comment">&lt;!-- Vue全局过滤器实现 (传参 + 过滤器串联) --&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>计算后的时间: &#123;&#123;name | myslices&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="comment">// 配置全局过滤器</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">filter</span>(<span class="string">&#x27;myslices&#x27;</span>, <span class="keyword">function</span> (<span class="params">value</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> value.<span class="title function_">slice</span>(<span class="number">0</span>, <span class="number">4</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">time</span>: <span class="number">1687892086472</span>,</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;你好, 尚硅谷&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">computed</span>: &#123;</span><br><span class="line">        <span class="title function_">times</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="title function_">dayjs</span>(<span class="variable language_">this</span>.<span class="property">time</span>).<span class="title function_">format</span>(<span class="string">&#x27;YYYY年MM月DD日 HH:mm:ss&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">times1</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="title function_">dayjs</span>(<span class="variable language_">this</span>.<span class="property">time</span>).<span class="title function_">format</span>(<span class="string">&#x27;YYYY年MM月DD日 HH:mm:ss&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">filters</span>: &#123;</span><br><span class="line">        <span class="title function_">formater</span>(<span class="params">value, str = <span class="string">&#x27;YYYY年MM月DD日 HH:mm:ss&#x27;</span></span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(value)</span><br><span class="line">            <span class="keyword">return</span> <span class="title function_">dayjs</span>(value).<span class="title function_">format</span>(str)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="title function_">myslice</span>(<span class="params">value</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> value.<span class="title function_">slice</span>(<span class="number">0</span>, <span class="number">4</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;.box1&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;小城故事, 你好&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">本文作者:</span> <span class="post-copyright-info"><a href="https://webxc.eu.org">XC's Blog</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">本文链接:</span> <span class="post-copyright-info"><a href="https://webxc.eu.org/post/aw92/">https://webxc.eu.org/post/aw92/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明:</span> <span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://webxc.eu.org" target="_blank">XC's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Vue%E6%A1%86%E6%9E%B6%E7%AC%94%E8%AE%B0/">Vue框架笔记</a></div><div class="post_share"><div class="social-share" data-image="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" alt="Wechat"></a><div class="post-qr-code-desc">Wechat</div></li><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" alt="Alipay"></a><div class="post-qr-code-desc">Alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/post/ou67/"><img class="prev-cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇文章</div><div class="prev_info">Vue第一天</div></div></a></div><div class="next-post pull-right"><a href="/post/ew32/"><img class="next-cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇文章</div><div class="next_info">Vue第三天</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/post/465190731/" title="VueX-数组对象的双向数据绑定"><img class="cover" src="https://npm.elemecdn.com/webxc@1.0.0/cover/vuex.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-01-03</div><div class="title">VueX-数组对象的双向数据绑定</div></div></a></div><div><a href="/post/ou67/" title="Vue第一天"><img class="cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-14</div><div class="title">Vue第一天</div></div></a></div><div><a href="/post/fn32/" title="Vue框架笔记"><img class="cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-05</div><div class="title">Vue框架笔记</div></div></a></div></div></div><hr><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i> <span>评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="author-info__name">XC's Blog</div><div class="author-info__description">XC's Blog 记录生活和学习文章</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">95</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://tenapi.cn/qqzlk/?qq=2848141597" target="_blank" title="QQ"><i class="iconfont icon-QQ"></i></a><a class="social-icon" href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.jpg" target="_blank" title="微信"><i class="iconfont icon-weixin"></i></a><a class="social-icon" href="https://github.com/web-xc" target="_blank" title="Github"><i class="iconfont icon-github"></i></a><a class="social-icon" href="https://space.bilibili.com/2134826455" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili"></i></a><a class="social-icon" href="https://music.163.com/#/user/home?id=323682437" target="_blank" title="网易云音乐"><i class="iconfont icon-wyy"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">原网站因使用.ml域名被回收, 再加上本人因学习原因, 没怎么打理博客, 使用了https://webxc.eu.org域名来维持博客运行, 如果有加友联的朋友们, 去友联页看信息即可, 如果原来有加过的, 麻烦在原来域名后缀改一下(.ml ==> .eu.org)就行, 博客写的太乱了, 没时间打理, 兄弟们谅解😁😁~~</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%A4%A9%E6%B0%94%E6%A1%88%E4%BE%8B-%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E5%AE%9E%E7%8E%B0-computed-methods"><span class="toc-number">1.</span> <span class="toc-text">1. 天气案例-计算属性实现-computed-methods</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E5%A4%A9%E6%B0%94%E6%A1%88%E4%BE%8B-%E7%9B%91%E5%90%AC%E5%B1%9E%E6%80%A7watch%E5%AE%9E%E7%8E%B0-immediate"><span class="toc-number">2.</span> <span class="toc-text">2. 天气案例-监听属性watch实现-immediate</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%A4%A9%E6%B0%94%E6%A1%88%E4%BE%8B-%E6%B7%B1%E5%BA%A6%E7%9B%91%E8%A7%86%E5%AE%9E%E7%8E%B0-watch-deep"><span class="toc-number">3.</span> <span class="toc-text">3. 天气案例-深度监视实现-watch-deep</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%A4%A9%E6%B0%94%E6%A1%88%E4%BE%8B-%E6%B7%B1%E5%BA%A6%E7%9B%91%E5%90%AC%E7%AE%80%E5%86%99"><span class="toc-number">4.</span> <span class="toc-text">4. 天气案例-深度监听简写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-computed%E5%92%8Cwatch%E4%B9%8B%E9%97%B4%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">5.</span> <span class="toc-text">5. computed和watch之间的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-Vue%E7%BB%91%E5%AE%9Aclass%E6%A0%B7%E5%BC%8F-class"><span class="toc-number">6.</span> <span class="toc-text">6. Vue绑定class样式 :class</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-Vue%E7%BB%91%E5%AE%9Astyle%E6%A0%B7%E5%BC%8F-style"><span class="toc-number">7.</span> <span class="toc-text">7. Vue绑定style样式 :style</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-Vue%E6%9D%A1%E4%BB%B6%E6%B8%B2%E6%9F%93v-show-v-else-if-template%E6%A8%A1%E6%9D%BF"><span class="toc-number">8.</span> <span class="toc-text">8. Vue条件渲染v-show-v-else-if-template模板</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-v-show%E5%86%99%E6%B3%95-v-show-x3D-%E2%80%9D%E8%A1%A8%E8%BE%BE%E5%BC%8F%E2%80%9D"><span class="toc-number">8.1.</span> <span class="toc-text">1. v-show写法: v-show&#x3D;”表达式”</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-v-if-else%E5%86%99%E6%B3%95"><span class="toc-number">8.2.</span> <span class="toc-text">2. v-if-else写法:</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-Vue%E5%88%97%E8%A1%A8%E6%B8%B2%E6%9F%93v-for%E6%8C%87%E4%BB%A4"><span class="toc-number">9.</span> <span class="toc-text">9. Vue列表渲染v-for指令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-Vue%E5%88%97%E8%A1%A8%E6%B8%B2%E6%9F%93v-for%E7%9A%84key%E4%BD%9C%E7%94%A8%E4%B8%8E%E5%8E%9F%E7%90%86"><span class="toc-number">10.</span> <span class="toc-text">10. Vue列表渲染v-for的key作用与原理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E8%99%9A%E6%8B%9FDOM%E4%B8%ADkey%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="toc-number">10.1.</span> <span class="toc-text">1. 虚拟DOM中key的作用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%AF%B9%E6%AF%94%E8%A7%84%E5%88%99"><span class="toc-number">10.2.</span> <span class="toc-text">2. 对比规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E7%94%A8index%E4%BD%9C%E4%B8%BAkey%E5%8F%AF%E8%83%BD%E4%BC%9A%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">10.3.</span> <span class="toc-text">3. 用index作为key可能会引发的问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%BC%80%E5%8F%91%E4%B8%AD%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9key"><span class="toc-number">10.4.</span> <span class="toc-text">4. 开发中如何选择key?</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E9%9D%A2%E8%AF%95%E9%A2%98-React%E3%80%81Vue%E4%B8%AD%E7%9A%84key%E6%9C%89%E4%BB%80%E4%B9%88%E4%BD%9C%E7%94%A8-key%E5%86%85%E9%83%A8%E5%8E%9F%E7%90%86"><span class="toc-number">10.5.</span> <span class="toc-text">5. 面试题: React、Vue中的key有什么作用? (key内部原理)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-Vue%E7%9A%84%E5%88%97%E8%A1%A8%E8%BF%87%E6%BB%A4"><span class="toc-number">11.</span> <span class="toc-text">11. Vue的列表过滤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-Vue%E7%9A%84%E5%88%97%E8%A1%A8%E8%BF%87%E6%BB%A4%E4%B8%8E%E5%88%97%E8%A1%A8%E6%8E%92%E5%BA%8F"><span class="toc-number">12.</span> <span class="toc-text">12. Vue的列表过滤与列表排序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-data%E6%95%B0%E6%8D%AE%E9%87%8C%E7%9A%84%E6%95%B0%E7%BB%84%E6%9B%B4%E6%96%B0%E6%97%B6%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98"><span class="toc-number">13.</span> <span class="toc-text">13. data数据里的数组更新时的一个问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-Vue%E6%A3%80%E6%B5%8B%E6%95%B0%E6%8D%AE%E6%94%B9%E5%8F%98%E5%8E%9F%E7%90%86"><span class="toc-number">14.</span> <span class="toc-text">14. Vue检测数据改变原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15-%E6%A8%A1%E6%8B%9FVue%E5%AF%B9%E8%B1%A1%E6%95%B0%E6%8D%AE%E7%9B%91%E6%B5%8B"><span class="toc-number">15.</span> <span class="toc-text">15. 模拟Vue对象数据监测</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16-Vue-set%E6%96%B9%E6%B3%95%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">16.</span> <span class="toc-text">16. Vue.set方法的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#17-%E6%A8%A1%E6%8B%9FVue%E6%95%B0%E7%BB%84%E6%95%B0%E6%8D%AE%E7%9B%91%E6%B5%8B%E4%B8%8E%E5%8E%9F%E7%90%86"><span class="toc-number">17.</span> <span class="toc-text">17. 模拟Vue数组数据监测与原理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E7%AC%AC%E4%B8%80%E7%A7%8D%E6%96%B9%E6%A1%88"><span class="toc-number">17.1.</span> <span class="toc-text">1. 第一种方案</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E7%AC%AC%E4%BA%8C%E7%A7%8D%E6%96%B9%E6%A1%88"><span class="toc-number">17.2.</span> <span class="toc-text">2. 第二种方案</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#18-%E6%80%BB%E7%BB%93Vue%E7%9B%91%E6%B5%8B%E6%95%B0%E6%8D%AE%E5%8E%9F%E7%90%86-x2F-%E5%AF%B9%E8%B1%A1-x2F-%E6%95%B0%E7%BB%84"><span class="toc-number">18.</span> <span class="toc-text">18. 总结Vue监测数据原理&#x2F;对象&#x2F;数组</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-Vue%E7%9B%91%E6%B5%8B%E6%95%B0%E6%8D%AE%E7%9A%84%E5%8E%9F%E7%90%86-Vue%E4%BC%9A%E7%9B%91%E8%A7%86data%E4%B8%AD%E6%89%80%E6%9C%89%E5%B1%82%E6%AC%A1%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-number">18.0.1.</span> <span class="toc-text">1. Vue监测数据的原理: Vue会监视data中所有层次的数据</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%A6%82%E4%BD%95%E7%9B%91%E6%B5%8B%E5%AF%B9%E8%B1%A1%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-number">18.1.</span> <span class="toc-text">2. 如何监测对象中的数据?</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%A6%82%E4%BD%95%E7%9B%91%E6%B5%8B%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-number">18.2.</span> <span class="toc-text">3. 如何监测数组中的数据?</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%9C%A8Vue%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9F%90%E4%B8%AA%E5%85%83%E7%B4%A0%E4%B8%80%E5%AE%9A%E8%A6%81%E7%94%A8%E4%BB%A5%E4%B8%8B%E6%96%B9%E6%B3%95"><span class="toc-number">18.3.</span> <span class="toc-text">4. 在Vue修改数组中的某个元素一定要用以下方法:</span></a></li></ol><li class="toc-item toc-level-2"><a class="toc-link" href="#19-Vue%E7%9A%84%E6%94%B6%E9%9B%86%E8%A1%A8%E5%8D%95%E6%95%B0%E6%8D%AE-v-model-lazy-number-trim"><span class="toc-number">19.</span> <span class="toc-text">19. Vue的收集表单数据-v-model-lazy-number-trim</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#20-Vue%E7%9A%84%E8%BF%87%E6%BB%A4%E5%99%A8-filter-dayjs%E5%BA%93"><span class="toc-number">20.</span> <span class="toc-text">20. Vue的过滤器-filter-dayjs库</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%87%E6%BB%A4%E5%99%A8%E8%AF%AD%E6%B3%95"><span class="toc-number">20.1.</span> <span class="toc-text">过滤器语法:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%87%E6%B3%A8"><span class="toc-number">20.2.</span> <span class="toc-text">备注:</span></a></li></ol></li></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/post/ds2s/" title="整个前端面试题"><img src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="整个前端面试题"></a><div class="content"><a class="title" href="/post/ds2s/" title="整个前端面试题">整个前端面试题</a><time datetime="2023-08-31T02:00:00.000Z" title="发表于 2023-08-31 02:00:00">2023-08-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/ds23/" title="常见前端面试题"><img src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="常见前端面试题"></a><div class="content"><a class="title" href="/post/ds23/" title="常见前端面试题">常见前端面试题</a><time datetime="2023-08-28T18:30:00.000Z" title="发表于 2023-08-28 18:30:00">2023-08-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/fe33/" title="硅谷甄选运营平台"><img src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="硅谷甄选运营平台"></a><div class="content"><a class="title" href="/post/fe33/" title="硅谷甄选运营平台">硅谷甄选运营平台</a><time datetime="2023-07-26T03:55:00.000Z" title="发表于 2023-07-26 03:55:00">2023-07-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2023 By XC's Blog</div><div class="framework-info"><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/?keyword=20235221">萌ICP备20235221号</a><span class="footer-separator">|</span><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/">萌ICP查询</a><div id="running-time"></div></div><script>setInterval((()=>{let e=Math.round(new Date("2021-11-21 01:35:06").getTime()/1e3),t=Math.round(((new Date).getTime()+288e5)/1e3)-e,n=new Array(0,0,0,0,0);t>=31536e3&&(n[0]=parseInt(t/31536e3),t%=31536e3),t>=86400&&(n[1]=parseInt(t/86400),t%=86400),t>=3600&&(n[2]=parseInt(t/3600),t%=3600),t>=60&&(n[3]=parseInt(t/60),t%=60),t>0&&(n[4]=t),currentTimeHtml="本站已安全运行 "+n[0]+" 年 "+n[1]+" 天 "+n[2]+" 时 "+n[3]+" 分 "+n[4]+" 秒",document.getElementById("running-time").innerHTML=currentTimeHtml}),1e3)</script></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i><span id="percent">0<span>%</span></span></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight,500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i> <span>正在载入文件,请稍后</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"></div></div><hr><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="https://npm.elemecdn.com/hexo-theme-butterfly/source/js/utils.js"></script><script src="https://npm.elemecdn.com/hexo-theme-butterfly@4.4.0/source/js/main.js"></script><script src="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://npm.elemecdn.com/instant.page/instantpage.js" type="module"></script><script src="https://npm.elemecdn.com/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.js"></script><script src="https://npm.elemecdn.com/hexo-theme-butterfly/source/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{const e=document.querySelectorAll("#article-container .mermaid-wrap");if(e.length){window.runMermaid=()=>{window.loadMermaid=!0;const t="dark"===document.documentElement.getAttribute("data-theme")?"dark":"default";Array.from(e).forEach(((e,n)=>{const d=e.firstElementChild,r="mermaid-"+n,m="%%{init:{ 'theme':'"+t+"'}}%%\n"+d.textContent;mermaid.mermaidAPI.render(r,m,(e=>{d.insertAdjacentHTML("afterend",e)}))}))};const t=()=>{window.loadMermaid?runMermaid():getScript("https://npm.elemecdn.com/mermaid/dist/mermaid.min.js").then(runMermaid)};window.pjax?t():document.addEventListener("DOMContentLoaded",t)}})()</script><script>(()=>{const o=()=>{twikoo.init(Object.assign({el:"#twikoo-wrap",envId:"https://blog2.webxc.eu.org/",region:"",onCommentLoaded:function(){btf.loadLightbox(document.querySelectorAll("#twikoo .tk-content img:not(.tk-owo-emotion)"))}},null))},t=()=>{"object"!=typeof twikoo?getScript("https://npm.elemecdn.com/twikoo@1.6.16/dist/twikoo.all.min.js").then(o):setTimeout(o,0)};t()})()</script></div><script data-pjax="false" defer src="https://npm.elemecdn.com/webxc@1.0.16/css/style.js"></script><script defer src="https://npm.elemecdn.com/hexo-theme-butterfly@4.4.0/source/js/search/local-search.js"></scrip><script src="https://npm.elemecdn.com/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!0,POWERMODE.mobile=!0,document.body.addEventListener("input",POWERMODE)</script><script src="https://npm.elemecdn.com/pjax@0.2.8/pjax.min.js"></script><script>let pjaxSelectors=["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax",".tk-comments-container"];var pjax=new Pjax({elements:'a:not([target="_blank"])',selectors:pjaxSelectors,cacheBust:!1,analytics:!1,scrollRestoration:!1});document.addEventListener("pjax:send",(function(){if(window.tocScrollFn&&window.removeEventListener("scroll",window.tocScrollFn),window.scrollCollect&&window.removeEventListener("scroll",scrollCollect),"object"==typeof preloader&&preloader.initLoading(),document.getElementById("rightside").style.cssText="opacity: ''; transform: ''",window.aplayers)for(let e=0;e<window.aplayers.length;e++)window.aplayers[e].options.fixed||window.aplayers[e].destroy();"object"==typeof typed&&typed.destroy();const e=document.body.classList;e.contains("read-mode")&&e.remove("read-mode"),"object"==typeof disqusjs&&disqusjs.destroy()})),document.addEventListener("pjax:complete",(function(){window.refreshFn(),document.querySelectorAll("script[data-pjax]").forEach((e=>{const t=document.createElement("script"),o=e.text||e.textContent||e.innerHTML||"";Array.from(e.attributes).forEach((e=>t.setAttribute(e.name,e.value))),t.appendChild(document.createTextNode(o)),e.parentNode.replaceChild(t,e)})),GLOBAL_CONFIG.islazyload&&window.lazyLoadInstance.update(),"function"==typeof chatBtnFn&&chatBtnFn(),"function"==typeof panguInit&&panguInit(),"function"==typeof gtag&&gtag("config","",{page_path:window.location.pathname}),"object"==typeof _hmt&&_hmt.push(["_trackPageview",window.location.pathname]),"function"==typeof loadMeting&&document.getElementsByClassName("aplayer").length&&loadMeting(),"object"==typeof Prism&&Prism.highlightAll(),"object"==typeof preloader&&preloader.endLoading()})),document.addEventListener("pjax:error",(e=>{404===e.request.status&&pjax.loadUrl("/404.html")}))</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script data-pjax>function butterfly_swiper_injector_config(){var s=document.getElementById("recent-posts");console.log("已挂载butterfly_swiper"),s.insertAdjacentHTML("afterbegin",'<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-08-31</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ds2s/&quot;);" href="javascript:void(0);" alt="">整个前端面试题</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-08-28</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ds23/&quot;);" href="javascript:void(0);" alt="">常见前端面试题</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-26</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe33/&quot;);" href="javascript:void(0);" alt="">硅谷甄选运营平台</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-17</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ew32/&quot;);" href="javascript:void(0);" alt="">Vue第三天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-16</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/aw92/&quot;);" href="javascript:void(0);" alt="">Vue第二天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-14</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ou67/&quot;);" href="javascript:void(0);" alt="">Vue第一天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-05</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fn32/&quot;);" href="javascript:void(0);" alt="">Vue框架笔记</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-21</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe11/&quot;);" href="javascript:void(0);" alt="">Vue前置JS功底</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/promise.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-11</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/pr67/&quot;);" href="javascript:void(0);" alt="">ES6-Promise语法与原理</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-08</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/or01/&quot;);" href="javascript:void(0);" alt="">Ajax第四天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-07</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fs67/&quot;);" href="javascript:void(0);" alt="">Ajax第三天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-05-11</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe66/&quot;);" href="javascript:void(0);" alt="">JS进阶第四天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-05-10</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe99/&quot;);" href="javascript:void(0);" alt="">JS进阶第三天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-05-05</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/us87/&quot;);" href="javascript:void(0);" alt="">JS进阶第二天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover15.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-10-31</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/2db5/&quot;);" href="javascript:void(0);" alt="">移动开发-Flex布局</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover16.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-11-14</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/1c7e/&quot;);" href="javascript:void(0);" alt="">移动开发-媒体查询</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover17.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-11-17</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/7fc/&quot;);" href="javascript:void(0);" alt="">移动开发-响应式</a><div class="blog-slider__text">点击查看</div></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div><div class="swiper-button-prev"></div><div class="swiper-button-next"></div></div></div>')}for(var elist="null".split(","),cpage=location.pathname,epage="all",flag=0,i=0;i<elist.length;i++)cpage.includes(elist[i])&&flag++;("all"===epage&&0==flag||epage===cpage)&&butterfly_swiper_injector_config()</script><script defer src="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper_init.js"></script></body></html>